package com.xyz.controller;

import com.xyz.aop.ObjectParam;
import com.xyz.aop.Param;
import com.xyz.config.SystemData;
import com.xyz.dto.*;
import com.xyz.entity.vo.RepairWorkOrderExportVo;
import com.xyz.entity.vo.UpkeepWorkOrderExportVo;
import com.xyz.service.CommonService;
import com.xyz.service.UpkeepAppService;
import com.xyz.service.UpkeepService;
import com.xyz.util.BeanUtils;
import com.xyz.util.ExcelUtils;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.GridData;
import com.xyz.util.dto.LoginInfo;
import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("upkeep")
public class UpkeepController {

    private static final Logger logger = LoggerFactory.getLogger(UpkeepController.class);

    @Autowired
    private UpkeepService upkeepService;

    @Autowired
    private UpkeepAppService upkeepAppService;

    @Autowired
    private CommonService commonService;

    /*
     * 列表查询
     * */
    @PostMapping("/getUpkeepAll")
    public DataResult getUpkeepAll(LoginInfo loginInfo, @ObjectParam SelectPageUpkeepDto selectPageUpkeepDto) {
        try {
            return upkeepAppService.getUpkeepAll(loginInfo, selectPageUpkeepDto);
        } catch (Exception e) {
            logger.error("getUpkeepAll", e);
            return DataResult.build9500();
        }
    }

    @ApiModelProperty("导出维保列表")
    @PostMapping("/exportData")
    public DataResult exportData(LoginInfo loginInfo, @ObjectParam SelectPageUpkeepDto selectPageUpkeepDto, HttpServletResponse response) {
        try {
            DataResult dataResult = upkeepAppService.getUpkeepAll(loginInfo, selectPageUpkeepDto);
            Object obj = dataResult.getInfo();
            List<UpkeepWorkOrderExportVo> list = new ArrayList<>();
            if (obj != null && obj instanceof GridData) {
                GridData gridData = (GridData) obj;
                list = BeanUtils.toBean(gridData.getRows(), UpkeepWorkOrderExportVo.class);
            }
            ExcelUtils.write(response, "维保管理.xls", "维保管理", UpkeepWorkOrderExportVo.class,
                    list);
            return DataResult.build9100();
        } catch (Exception e) {
            logger.error("getUpkeepAll", e);
            return DataResult.build9500();
        }
    }


    /*
     * 新增保养
     * */
    @PostMapping("/insertUpkeep")
    public DataResult insertUpkeep(LoginInfo loginInfo, @ObjectParam UpkeepWorkOrderDto dto) {
        try {
            synchronized (SystemData.upkeepLock) {
                return upkeepService.insertUpkeep(loginInfo, dto);
            }
        } catch (Exception e) {
            logger.error("insertRepair", e);
            return DataResult.build9500();
        }
    }

    /*
     * 编辑保养
     * */
    @PostMapping("/updateUpkeep")
    public DataResult updateUpkeep(LoginInfo loginInfo, @ObjectParam UpkeepWorkOrderDto dto) {
        try {
            return upkeepService.updateUpkeep(loginInfo, dto);
        } catch (Exception e) {
            logger.error("updateUpkeep", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据ID查询报维保详情
     * */
    @PostMapping("/getUpkeepBasisById")
    public DataResult getUpkeepBasisById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String upkeepId
    ) {
        try {
            return upkeepAppService.getUpkeepBasisById(loginInfo, upkeepId);
        } catch (Exception e) {
            logger.error("getUpkeepBasisById", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据ID修改报维保详情
     * */
    @PostMapping("/getUpkeepBasisUpdateById")
    public DataResult getUpkeepBasisUpdateById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String upkeepId,
            @Param(isRequired = false) String maintenEmployeeUsersId
    ) {
        try {
            return upkeepAppService.getUpkeepBasisUpdateById(loginInfo, upkeepId,maintenEmployeeUsersId);
        } catch (Exception e) {
            logger.error("getUpkeepBasisById", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据ID查询维保审核信息
     * */
    @PostMapping("/getAnnounceUpkeepById")
    public DataResult getAnnounceUpkeepById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String upkeepId
    ) {
        try {
            return upkeepAppService.getAnnounceUpkeepById(loginInfo, upkeepId);
        } catch (Exception e) {
            logger.error("getAnnounceUpkeepById", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据ID查询报保审维修信息
     * */
    @PostMapping("/getMaintenanceUpkeepById")
    public DataResult getMaintenanceUpkeepById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String upkeepId
    ) {
        try {
            return upkeepAppService.getMaintenanceUpkeepById(loginInfo, upkeepId);
        } catch (Exception e) {
            logger.error("getMaintenanceUpkeepById", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据ID查询验收信息
     * */
    @PostMapping("/getAcceptUpkeepById")
    public DataResult getAcceptUpkeepById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String upkeepId
    ) {
        try {
            return upkeepAppService.getAcceptUpkeepById(loginInfo, upkeepId);
        } catch (Exception e) {
            logger.error("getAcceptUpkeepById", e);
            return DataResult.build9500();
        }
    }

    /*
     * 工单流水
     * */
    @PostMapping("/getUpkeepWorkOrderFlow")
    public DataResult getUpkeepWorkOrderFlow(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String upkeepId
    ) {
        try {
            return upkeepAppService.getUpkeepWorkOrderFlow(loginInfo, upkeepId);
        } catch (Exception e) {
            logger.error("getUpkeepWorkOrderFlow", e);
            return DataResult.build9500();
        }
    }

    /*
     * 领取工单
     * receiveType = 1 自己领取
     * receiveType = 2 领导派单
     * */
    @PostMapping("/receiveUpkeep")
    public DataResult receiveUpkeep(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String upkeepId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String receiveType,
            @Param(type = Param.ParamType.POSITIVE_INTEGER, isRequired = false) String employeeId
    ) {
        try {
            return upkeepService.receiveUpkeep(loginInfo, upkeepId, receiveType, employeeId);
        } catch (Exception e) {
            logger.error("receiveUpkeep", e);
            return DataResult.build9500();
        }
    }

    /*
     * 拒单
     * */
    @PostMapping("/refuseUpkeep")
    public DataResult refuseUpkeep(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return upkeepService.refuseUpkeep(loginInfo, id);
        } catch (Exception e) {
            logger.error("refuseUpkeep", e);
            return DataResult.build9500();
        }
    }

    /*
     * 手动点击维保中
     * */
    @PostMapping("/undeUpkeep")
    public DataResult undeUpkeep(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return upkeepService.undeUpkeep(loginInfo, id);
        } catch (Exception e) {
            logger.error("undeUpkeep", e);
            return DataResult.build9500();
        }
    }

    /*
     * 暂停和开始维保
     * type;1,暂停；2开始维修
     * */
    @PostMapping("/suspendUpkeep")
    public DataResult suspendUpkeep(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String type
    ) {
        try {
            return upkeepService.suspendUpkeep(loginInfo, id, type);
        } catch (Exception e) {
            logger.error("suspendUpkeep", e);
            return DataResult.build9500();
        }
    }

    /*
     * 工单转交
     * */
    @PostMapping("/transferUpkeep")
    public DataResult transferUpkeep(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return upkeepService.transferUpkeep(loginInfo, id);
        } catch (Exception e) {
            logger.error("transferUpkeep", e);
            return DataResult.build9500();
        }
    }

    /*
     * 维保保存
     * */
    @PostMapping("/insertSuspendUpkeep")
    public DataResult insertSuspendUpkeep(
            LoginInfo loginInfo,
            @ObjectParam UpkeepMaintenanceInsDto upkeepMaintenanceInsDto
    ) {
        try {
            return upkeepService.insertSuspendUpkeep(loginInfo, upkeepMaintenanceInsDto);
        } catch (Exception e) {
            logger.error("insertSuspendUpkeep", e);
            return DataResult.build9500();
        }
    }

    /*
     * 点击维保完成
     * */
    @PostMapping("/completeUpkeep")
    public DataResult completeUpkeep(
            LoginInfo loginInfo,
            @ObjectParam UpkeepMaintenanceInsDto upkeepMaintenanceInsDto
    ) {
        try {
            return upkeepService.completeUpkeep(loginInfo, upkeepMaintenanceInsDto);
        } catch (Exception e) {
            logger.error("completeUpkeep", e);
            return DataResult.build9500();
        }
    }

    /*
     * 验收指派人员
     * */
    @PostMapping("/accepAssignUpkeep")
    public DataResult accepAssignUpkeep(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String employeeId
    ) {
        try {
            return upkeepService.accepAssignUpkeep(loginInfo, id, employeeId);
        } catch (Exception e) {
            logger.error("accepAssignUpkeep", e);
            return DataResult.build9500();
        }
    }

    /**
     * 验收
     **/
    @PostMapping("/acceptUpkeep")
    public DataResult acceptUpkeep(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String type,
            @Param(isRequired = false) String acceptExplain,
            @Param(isRequired = false) String acceptScore
    ) {
        try {
            return upkeepService.acceptUpkeep(loginInfo, id, type, acceptExplain, acceptScore);
        } catch (Exception e) {
            logger.error("acceptUpkeep", e);
            return DataResult.build9500();
        }
    }

    /*
     * App待审核查询
     * */
    @PostMapping("/getPendingUpkeepAll")
    public DataResult getPendingUpkeepAll(LoginInfo loginInfo, @ObjectParam SelectPageUpkeepDto selectPageUpkeepDto) {
        try {
            return upkeepAppService.getPendingUpkeepAll(loginInfo, selectPageUpkeepDto);
        } catch (Exception e) {
            logger.error("getPendingUpkeepAll", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("/updateTask")
    public DataResult updateTask(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return upkeepService.updateTask(loginInfo, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("updateTask", e);
            return DataResult.build9500();
        }
    }


}
